package org.jupiter.blog.web.controller;

import org.jupiter.blog.api.dto.JupiterTagDTO;
import org.jupiter.blog.api.http.JupiterResult;
import org.jupiter.blog.service.service.JupiterTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/tags")
public class JupiterTagController {

	@Autowired
	private JupiterTagService tagService;

	@GetMapping("/{uuid}")
	public JupiterResult<JupiterTagDTO> getById(@PathVariable String uuid) {
		JupiterTagDTO tag = tagService.getById(uuid);
		return JupiterResult.success(tag);
	}

	@GetMapping
	public JupiterResult<List<JupiterTagDTO>> getAll() {
		List<JupiterTagDTO> tags = tagService.getAll();
		return JupiterResult.success(tags);
	}

	@GetMapping("/page")
	public JupiterResult<Object> getByPage(
			@RequestParam(defaultValue = "1") int page,
			@RequestParam(defaultValue = "10") int size,
			@RequestParam(required = false) String name) {

		//		List<JupiterTagDTO> tags = tagService.getByPage(page, size, name);
		//		long total = tagService.getCount(name);
		//todo
		return JupiterResult.success();
	}

	@PostMapping
	public JupiterResult<Void> create(@RequestBody JupiterTagDTO tag) {
		tagService.create(tag);
		return JupiterResult.success();
	}

	@PutMapping("/{uuid}")
	public JupiterResult<Void> update(@PathVariable String uuid, @RequestBody JupiterTagDTO tag) {
		tag.setUuid(uuid);
		tagService.update(tag);
		return JupiterResult.success();
	}

	@DeleteMapping("/{uuid}")
	public JupiterResult<Void> delete(@PathVariable String uuid) {
		tagService.delete(uuid);
		return JupiterResult.success();
	}

	@PutMapping("/{uuid}/sort")
	public JupiterResult<Void> updateSort(@PathVariable String uuid, @RequestParam Integer sort) {
		tagService.updateSort(uuid, sort);
		return JupiterResult.success();
	}

	@GetMapping("/active")
	public JupiterResult<List<JupiterTagDTO>> getActiveTags() {
		List<JupiterTagDTO> tags = tagService.getActiveTags();
		return JupiterResult.success(tags);
	}

	@GetMapping("/article/{articleId}")
	public JupiterResult<List<JupiterTagDTO>> getTagsByArticleId(@PathVariable String articleId) {
		List<JupiterTagDTO> tags = tagService.getTagsByArticleId(articleId);
		return JupiterResult.success(tags);
	}
}

